/**
 * @descript 公司运营报表
 */

var _select_target = 1,
	_select_cols = [],
	_isCreateTable = false,
	$tableList;
$(function() {
	$tableList = $('#tableList');
	//修改表格高度
	var h = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
	$tableList.attr({
		'data-height': h - 156
	});
	$('#startdate').val(new Date().addDays(-1).format('yyyy-MM-dd'));
	$('#enddate').val(new Date().format('yyyy-MM-dd'));
	$('#navList a').on('click', function(e) {
		var tar = $(e.target),
			t = tar.attr('target'),
			p = tar.parent();
		_select_target = parseInt(t);
		_isCreateTable = false; //切换的时候表格需要重新创建
		$('#navList li').removeClass('active');
		p.addClass('active');
		//$('#startdate,#enddate').val(new Date().addDays(-1).format('yyyy-MM-dd'));
		getDataForTable(_select_target);
	});
	$('#btnSearch').on('click', function() {
		getDataForTable(_select_target);
	});
	$(".datepicker").datepicker({
		format: "yyyy-MM-dd"
	});
	getDataForTable(_select_target);
});
/**
 * 获取表格数据
 * @param {Object} code 获取的数据类型
 */
function getDataForTable(code) {
		getDataInterFace({
			code: code,
			startdate: $('#startdate').val(),
			enddate: $('#enddate').val()
		}, setDataToTale);
	}
	/**
	 * 绑定数据到表格上
	 */

function setDataToTale(data) {
		//data = data ? $.parseJSON(data) : {};
		data = data || {
			info: []
		};

		_isCreateTable ? ($tableList.bootstrapTable('destroy'), $tableList.bootstrapTable({
			data: data.info
		})) : (createTable(), $tableList.bootstrapTable({
			data: data.info
		}));
		//$('#tableList').bootstrapTable('load',data.info);
		//$('#tableList').bootstrapTable('hideLoading');
	}
	/**
	 * 创建表格
	 */

function createTable() {
	$tableList.bootstrapTable('destroy');
	_isCreateTable = true;
	var cols = _COLUMNS_TEXT[_select_target],
		tCols = [];
	_select_cols = [];
	for (var k in cols) {
		if (cols[k].isshow) {
			tCols.push($('<th>').attr({
				'data-field': k,
				'data-halign': cols[k]['halign'] || 'center',
				'data-align': cols[k]['align'] || 'center',
				'data-formatter': cols[k]['formatter'] || ''
			}).html(cols[k]['text']));
			_select_cols.push(k);
		}
	}
	$('thead tr', $tableList).empty().append(tCols);
}